#include "designitem.h"

DesignItem::DesignItem(QObject *parent) :
    AbstractModelItem(parent),
    d(new Data())
{
}

DesignItem::DesignItem(DataContainer *container, QObject *parent) :
    AbstractModelItem(parent),
    d(new Data())
{
    setContainer(container);
}

DesignItem::~DesignItem()
{
    if (d->dataContainer)
    {
        d->dataContainer->deleteLater();
    }
}

DesignItem *DesignItem::fromData(DataContainer *container)
{
//    qDebug() << Q_FUNC_INFO << container->metaObject()->className() << "is a data container!";

    DesignItem* item = new DesignItem(container);
    foreach (QObject* child, container->children())
    {
        DataContainer* childContainer = qobject_cast<DataContainer*>(child);
        if (!childContainer)
        {
            qDebug() << Q_FUNC_INFO << child->metaObject()->className() << "is not a data container!";
            continue;
        }

        DesignItem* childItem = fromData(childContainer);
        childItem->setParent(item);
    }

    return item;
}

int DesignItem::fieldCount() const
{
    if (d->dataContainer.isNull())
    {
        return 1;
    }

    return d->dataContainer->fieldCount()+1;
}

QVariant DesignItem::value(int field, int role) const
{
    if (role != Qt::DisplayRole && role != Qt::EditRole)
    {
        return QVariant();
    }

    if (d->dataContainer.isNull())
    {
        return QVariant();
    }

    if (field <= 0)
    {
        return d->dataContainer->metaObject()->className();
    }

    field = field-1;
//    if (field < d->dataContainer->internalFieldCount())
//    {
//        return d->dataContainer->value(field, role);
//    }

    if (field >= d->dataContainer->fieldCount())
    {
        return QVariant();
    }

    QVariant ret = d->dataContainer->fieldName(field);

    return ret;
}

bool DesignItem::setValue(int field, const QVariant &value, int role)
{
    if (role != Qt::EditRole)
    {
        return false;
    }

    if (d->dataContainer.isNull())
    {
        qDebug() << Q_FUNC_INFO << "Data container is null";
        return false;
    }

    if (field <= 0)
    {
        return false;
    }

    field = field-1;

    if (field < d->dataContainer->internalFieldCount())
    {
        return false;

//        return d->dataContainer->setValue(field, value, role);
    }

    QString name = value.toString();
    if (name.contains(" "))
    {
        return false;
    }

//    qDebug() << Q_FUNC_INFO << "Set container field name" << value.toString();
    return d->dataContainer->setFieldName(field, value.toString());
}

QVariant DesignItem::fieldName(int field) const
{
    return d->dataContainer->fieldName(field);
}

DataContainer *DesignItem::container() const
{
    return d->dataContainer;
}

void DesignItem::setContainer(DataContainer *container)
{
    if (!d->dataContainer.isNull())
    {
        return;
    }

    d->dataContainer = container;
    if (!parentItem())
    {
        return;
    }

    if (!parentItem()->container() || parentItem()->container() == d->dataContainer->parent())
    {
        return;
    }

    d->dataContainer->setParent(parentItem()->container());
}

DesignItem *DesignItem::parentItem() const
{
    return qobject_cast<DesignItem*>(parent());
}


